package com.xinke.module.system.controller.admin.user;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.user.*;
import com.xinke.module.system.model.entity.user.UserPostDO;
import com.xinke.module.system.service.user.UserPostService;

@Tag(name = "管理后台 - 用户和岗位关联")
@RestController
@RequestMapping("/system/user-post")
@Validated
public class UserPostController {

    @Resource
    private UserPostService userPostService;

    @PostMapping("/create")
    @Operation(summary = "创建用户和岗位关联")
    @PreAuthorize("@ss.hasPermission('system:user-post:create')")
    public Result<Long> createUserPost(@Valid @RequestBody UserPostSaveReqVO createReqVO) {
        return success(userPostService.createUserPost(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户和岗位关联")
    @PreAuthorize("@ss.hasPermission('system:user-post:update')")
    public Result<Boolean> updateUserPost(@Valid @RequestBody UserPostSaveReqVO updateReqVO) {
        userPostService.updateUserPost(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户和岗位关联")
    @Parameter(name = "userPostId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:user-post:delete')")
    public Result<Boolean> deleteUserPost(@RequestParam("userPostId") Long userPostId) {
        userPostService.deleteUserPost(userPostId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户和岗位关联")
    @Parameter(name = "userPostId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:user-post:query')")
    public Result<UserPostRespVO> getUserPost(@RequestParam("userPostId") Long userPostId) {
        UserPostDO userPost = userPostService.getUserPost(userPostId);
        return success(BeanUtils.toBean(userPost, UserPostRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户和岗位关联分页")
    @PreAuthorize("@ss.hasPermission('system:user-post:query')")
    public Result<PageResult<UserPostRespVO>> getUserPostPage(@Valid UserPostPageReqVO pageReqVO) {
        PageResult<UserPostDO> pageResult = userPostService.getUserPostPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserPostRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户和岗位关联 Excel")
    @PreAuthorize("@ss.hasPermission('system:user-post:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserPostExcel(@Valid UserPostPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserPostDO> list = userPostService.getUserPostPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户和岗位关联.xls", "数据", UserPostRespVO.class,
                        BeanUtils.toBean(list, UserPostRespVO.class));
    }

}